package com.tanhua.manage.service;

import com.tanhua.manage.domain.AnalysisByDay;
import com.tanhua.manage.mapper.AnalysisByDayMapper;
import com.tanhua.manage.utils.ComputeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

@Service
public class DashboardService {

    @Autowired(required = false)
    private AnalysisByDayMapper analysisByDayMapper;

    //新增、活跃用户、次日留存率
    public ResponseEntity<Object> dashboard(Long sd, Long ed, Integer type) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String start = format.format(sd);
        String end = format.format(ed);
        LocalDate startLocalDate = LocalDate.parse(start);
        startLocalDate = startLocalDate.minusYears(1);
        String startYes = startLocalDate.toString();
        LocalDate endLocalDate = LocalDate.parse(end);
        endLocalDate = endLocalDate.minusYears(1);
        String endYes = endLocalDate.toString();

        List<AnalysisByDay> analysisByDayNow = analysisByDayMapper.findByDate(start, end);
        List<AnalysisByDay> analysisByDayYes = analysisByDayMapper.findByDate(startYes, endYes);

        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> thisYear = new ArrayList<>();
        //101 新增 102 活跃用户 103 次日留存率
        for (AnalysisByDay analysisByDay : analysisByDayNow) {
            Date time = analysisByDay.getRecordDate();
            String title = format.format(time);
            while (!start.equals(title)) {
                start = addYear(start, thisYear);
            }

            start = title;
            Map<String, Object> today = new HashMap<>();
            today.put("title", title);
            switch (type) {
                case 101:
                    today.put("amount", analysisByDay.getNumRegistered());
                    break;
                case 102:
                    today.put("amount", analysisByDay.getNumActive());
                    break;
                case 103:
                    today.put("amount", analysisByDay.getNumRetention1d());
                    break;
            }
            thisYear.add(today);
        }

        while (!start.equals(end)) {
            start = addYear(start, thisYear);
        }

        map.put("thisYear", thisYear);

        List<Map<String, Object>> lastYear = new ArrayList<>();
        for (AnalysisByDay analysisByDay : analysisByDayYes) {

            Date time = analysisByDay.getRecordDate();
            String title = format.format(time);

            while (!startYes.equals(title)) {
                startYes = addYear(startYes, lastYear);
            }
            startYes = title;
            Map<String, Object> yes = new HashMap<>();
            yes.put("title", title);
            switch (type) {
                case 101:
                    yes.put("amount", analysisByDay.getNumRegistered());
                    break;
                case 102:
                    yes.put("amount", analysisByDay.getNumActive());
                    break;
                case 103:
                    yes.put("amount", analysisByDay.getNumRetention1d());
                    break;
            }
            lastYear.add(yes);
        }

        while (!startYes.equals(endYes)) {
            startYes = addYear(startYes, lastYear);
        }

        map.put("lastYear", lastYear);

        return ResponseEntity.ok(map);
    }

    //封装添加的方法
    public String addYear(String start, List<Map<String, Object>> oneTimeList) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Object> today = new HashMap<>();
        today.put("title", start);
        today.put("amount", 0);
        Date date = format.parse(start);
        start = ComputeUtil.offsetDay(date, 1);
        date = format.parse(start);
        start = format.format(date);
        oneTimeList.add(today);
        return start;
    }
}
